﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;




namespace DotNetNuke.Tests.UI.WatiN.HostTests
{
    [TestFixture]
    
    public class HostSearchAdminTests : HTBase
    {
        #region Test Setup and TearDown

        [SetUp]
        public void SetUp()
        {
            
            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Change_Minimum_Word_Length()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            bool usesRibbonBar = false;

            //Add some content to you website
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.PageNameField.Value = pageName;            
            newPage.PageTemplateSelect.Select("None Specified");
            newPage.AllUsersViewPermissionCheckbox.ClickNoWait();
            newPage.AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/testpage.aspx");
            

            string moduleContent = "This is a module for checking the host Search admin tests. These are some words to use for testing: tea, ambiguous, 42580, according. ";
            if (!usesRibbonBar)
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);
                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);
                }
            }
            else
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);

                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);

                }
            }

            //Act
            ieInstance.GoTo(SiteURL + "/tabid/29/portalid/0/Default.aspx");
            SearchAdminPage searchAdmin = new SearchAdminPage(homePage);
            searchAdmin.MinimumWordLengthField.Value = "4";
            searchAdmin.UpdateLink.Click();
            searchAdmin.ReIndexContentLink.Click();

            SiteMenu menu = new SiteMenu(homePage);
            menu.SearchField.Value = "tea";
            menu.SearchLink.Click();
            
            //Assert
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Sorry, no results were found for"));

            menu.SearchField.Value = "ambiguous";
            menu.SearchLink.Click();
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Search results for"));
            Assert.IsTrue(searchAdmin.SearchResultsSummarySpan.InnerHtml.Contains("This is a module for checking the host Search admin tests."));

        }


        [Test]
        public void Change_Maximum_Word_Length()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            bool usesRibbonBar = false;

            //Add some content to you website
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.PageNameField.Value = pageName;
            newPage.PageTemplateSelect.Select("None Specified");
            newPage.AllUsersViewPermissionCheckbox.ClickNoWait();
            newPage.AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/testpage.aspx");
           
            string moduleContent = "This is a module for checking the host Search admin tests. These are some words to use for testing: tea, ambiguous, 42580, according. ";
            if (!usesRibbonBar)
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);
                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);
                }
            }
            else
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);

                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);

                }
            }

            //Act
            ieInstance.GoTo(SiteURL + "/tabid/29/portalid/0/Default.aspx");
            SearchAdminPage searchAdmin = new SearchAdminPage(homePage);
            searchAdmin.MaximumWordLengthField.Value = "6";
            searchAdmin.UpdateLink.Click();
            System.Threading.Thread.Sleep(1500);
            searchAdmin.ReIndexContentLink.Click();

            SiteMenu menu = new SiteMenu(homePage);
            menu.SearchField.Value = "ambiguous";
            menu.SearchLink.Click();
            System.Threading.Thread.Sleep(1500);

            //Assert
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Sorry, no results were found for"));

            menu.SearchField.Value = "words";
            menu.SearchLink.Click();
            System.Threading.Thread.Sleep(1500);
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Search results for"));
            Assert.IsTrue(searchAdmin.SearchResultsSummarySpan.InnerHtml.Contains("This is a module for checking the host Search admin tests."));
        }

        [Test]
        public void Include_Common_Words()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            bool usesRibbonBar = false;

            //Add some content to you website
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.PageNameField.Value = pageName;
            newPage.PageTemplateSelect.Select("None Specified");
            newPage.AllUsersViewPermissionCheckbox.ClickNoWait();
            newPage.AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/testpage.aspx");

            string moduleContent = "This is a module for checking the host Search admin tests. These are some words to use for testing: tea, ambiguous, 42580, according. ";
            if (!usesRibbonBar)
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);
                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);
                }
            }
            else
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);

                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);

                }
            }

            //Act
            ieInstance.GoTo(SiteURL + "/tabid/29/portalid/0/Default.aspx");
            SearchAdminPage searchAdmin = new SearchAdminPage(homePage);
            searchAdmin.IncludeCommonWordsCheckbox.Checked = true;
            searchAdmin.UpdateLink.Click();
            searchAdmin.ReIndexContentLink.Click();

            //Assert
            SiteMenu menu = new SiteMenu(homePage);
            menu.SearchField.Value = "according";
            menu.SearchLink.Click();
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Search results for"));
            Assert.IsTrue(searchAdmin.SearchResultsSummarySpan.InnerHtml.Contains("This is a module for checking the host Search admin tests."));
        }

        [Test]
        public void Exclude_Common_Words()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            bool usesRibbonBar = false;

            //Add some content to you website
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.PageNameField.Value = pageName;
            newPage.PageTemplateSelect.Select("None Specified");
            newPage.AllUsersViewPermissionCheckbox.ClickNoWait();
            newPage.AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/testpage.aspx");
            
            string moduleContent = "This is a module for checking the host Search admin tests. These are some words to use for testing: tea, ambiguous, 42580, according. ";
            if (!usesRibbonBar)
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);
                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);
                }
            }
            else
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);

                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);

                }
            }

            //Act
            ieInstance.GoTo(SiteURL + "/tabid/29/portalid/0/Default.aspx");
            SearchAdminPage searchAdmin = new SearchAdminPage(homePage);
            searchAdmin.IncludeCommonWordsCheckbox.Checked = false;
            searchAdmin.UpdateLink.Click();
            searchAdmin.ReIndexContentLink.Click();

            //Assert
            SiteMenu menu = new SiteMenu(homePage);
            menu.SearchField.Value = "according";
            menu.SearchLink.Click();
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Sorry, no results were found for"));
        }


        [Test]
        public void Include_Numbers()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            bool usesRibbonBar = false;

            //Add some content to you website
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.PageNameField.Value = pageName;
            newPage.PageTemplateSelect.Select("None Specified");
            newPage.AllUsersViewPermissionCheckbox.ClickNoWait();
            newPage.AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/testpage.aspx");
           
            string moduleContent = "This is a module for checking the host Search admin tests. These are some words to use for testing: tea, ambiguous, 42580, according. ";
            if (!usesRibbonBar)
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);
                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);
                }
            }
            else
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);

                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);

                }
            }

            //Act
            ieInstance.GoTo(SiteURL + "/tabid/29/portalid/0/Default.aspx");
            SearchAdminPage searchAdmin = new SearchAdminPage(homePage);
            searchAdmin.IncludeNumbersCheckbox.Checked = true;
            searchAdmin.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            searchAdmin.ReIndexContentLink.ClickNoWait();

            //Assert
            System.Threading.Thread.Sleep(1500);
            SiteMenu menu = new SiteMenu(homePage);
            menu.SearchField.Value = "42580";
            menu.SearchLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Search results for"));
            Assert.IsTrue(searchAdmin.SearchResultsSummarySpan.InnerHtml.Contains("This is a module for checking the host Search admin tests."));
        }

        [Test]
        public void Exclude_Numbers()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            bool usesRibbonBar = false;

            //Add some content to you website
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.PageNameField.Value = pageName;
            newPage.PageTemplateSelect.Select("None Specified");
            newPage.AllUsersViewPermissionCheckbox.ClickNoWait();
            newPage.AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/testpage.aspx");

            string moduleContent = "This is a module for checking the host Search admin tests. These are some words to use for testing: tea, ambiguous, 42580, according. ";
            if (!usesRibbonBar)
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);
                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    controlPanel.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);
                }
            }
            else
            {
                if (VersionType.CompareTo("Community") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML", moduleContent, 0);

                }
                else if (VersionType.CompareTo("Professional") == 0 || VersionType.CompareTo("Enterprise") == 0)
                {
                    ribbonBar.AddHTMLModuleToPageWithContent("Test Module", "HTML Pro", moduleContent, 0);

                }
            }

            //Act
            ieInstance.GoTo(SiteURL + "/tabid/29/portalid/0/Default.aspx");
            SearchAdminPage searchAdmin = new SearchAdminPage(homePage);
            searchAdmin.IncludeNumbersCheckbox.Checked = false;
            searchAdmin.UpdateLink.Click();
            searchAdmin.ReIndexContentLink.Click();

            //Assert
            SiteMenu menu = new SiteMenu(homePage);
            menu.SearchField.Value = "42580";
            menu.SearchLink.Click();
            Assert.IsTrue(searchAdmin.SearchResultsMessageSpan.InnerHtml.Contains("Sorry, no results were found for"));
        }

        #endregion
    }
}
